package com.bibrApp.difu.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.OvershootInterpolator;


/**
 * Created by gengzhibo on 17/8/3.
 */
public class DashBoard extends View {


    private Paint paint, tmpPaint, textPaint, strokePain;
    private RectF rect;
    private int backGroundColor;    //背景色
    private float pointLength;      //指针长度
    private float per;             //指数百分比
    private float perPoint;        //缓存(变化中)指针百分比
    private float perOld;          //变化前指针百分比
    private float length;          //仪表盘半径
    private float r;

    private int maxValue = 100;

    public DashBoard(Context context) {
        super(context);
        init();
    }


    public DashBoard(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int heitht = width / 2 / 4 * 5;
        initIndex(width / 2);
        //优化组件高度
        setMeasuredDimension(width, heitht);
    }


    private void initIndex(int specSize) {
        backGroundColor = Color.WHITE;
        r = specSize;
        length = r / 4 * 3;
        pointLength = -(float) (r * 0.6);
        per = 0;
        perOld = 0;
    }


    private void init() {
        paint = new Paint();
        rect = new RectF();
        textPaint = new Paint();
        tmpPaint = new Paint();
        strokePain = new Paint();
    }

    public DashBoard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void setR(float r) {
        this.r = r;
        this.length = r / 4 * 3;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        setLayerType(LAYER_TYPE_SOFTWARE, null);
        //颜色指示的环
        initRing(canvas);
        //刻度文字
        initScale(canvas);
        //指针
        initPointer(canvas);
        //提示内容
        initText(canvas);
    }

    private void initText(Canvas canvas) {
        //抗锯齿
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        canvas.restore();
        canvas.save();
        canvas.translate(canvas.getWidth() / 2, r);

        paint.clearShadowLayer();

        canvas.restore();
        canvas.save();
        canvas.translate(canvas.getWidth() / 2f, r);


        textPaint.setStrokeWidth(2);
        textPaint.setAntiAlias(true);

        //判断指数变化及颜色设定
        int _per = (int) (per * maxValue);


        textPaint.setColor(getColor());

        float swidth = textPaint.measureText(String.valueOf(_per));
        //计算偏移量 是的数字和百分号整体居中显示
        swidth = (swidth - (swidth + 22) / 2);

        canvas.translate(swidth, 0);

        textPaint.setTextSize(40);
        textPaint.setTextAlign(Paint.Align.CENTER);

        canvas.restore();
        canvas.save();
        canvas.translate(canvas.getWidth() / 2, r + length / 3 / 2);
        canvas.drawText(_per + "%", 0, 0, textPaint);
    }


    public void setBackGroundColor(int color) {
        this.backGroundColor = color;
    }

    public void setPointLength1(float pointLength1) {
        this.pointLength = -length * pointLength1;
    }

    private void initScale(Canvas canvas) {
        canvas.restore();
        canvas.save();
        canvas.translate(canvas.getWidth() / 2, r);
        paint.setColor(Color.parseColor("#999999"));

        tmpPaint = new Paint(paint); //小刻度画笔对象
        tmpPaint.setStrokeWidth(1);
        tmpPaint.setTextSize(25);
        tmpPaint.setTextAlign(Paint.Align.CENTER);

        canvas.rotate(-90, 0f, 0f);

        float y = length;
        y = -y;
        paint.setColor(backGroundColor);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(5);

        canvas.drawText("0%", 0, y - 20f, tmpPaint);
        canvas.rotate(7*18, 0f, 0f);
        canvas.drawText("70%", 0, y - 20f, tmpPaint);
        canvas.rotate(27, 0f, 0f);
        canvas.drawText("85%", 0, y - 20f, tmpPaint);
        canvas.rotate(9, 0f, 0f);
        canvas.drawText("90%", 0, y - 20f, tmpPaint);
        canvas.rotate(18, 0f, 0f);
        canvas.drawText("100%", 0, y - 20f, tmpPaint);
    }


    private void initPointer(Canvas canvas) {
        paint.setColor(Color.BLACK);


        canvas.restore();
        canvas.save();
        canvas.translate(canvas.getWidth() / 2, r);
        float change;

        if (perPoint < 1) {
            change = perPoint * 180;
        } else {
            change = 180;
        }


        paint.setColor(getColor());
        //根据参数得到旋转角度
        canvas.rotate(-90 + change, 0f, 0f);

        //绘制三角形形成指针
        Path path = new Path();
        path.moveTo(0, pointLength);
        path.lineTo(-10, 0);
        path.lineTo(10, 0);
        path.lineTo(0, pointLength);
        path.close();

        canvas.drawPath(path, paint);

    }

    private int getColor() {
        int _per = (int) (per * maxValue);

        if (_per < 70) {
            return valueColors[0];
        } else if (_per < 85) {
            return valueColors[1];
        } else if (_per < 90) {
            return valueColors[2];
        } else {
            return valueColors[3];
        }
    }

    int[] valueColors = {
            Color.parseColor("#33bf90"),
            Color.parseColor("#eeb350"),
            Color.parseColor("#ff6600"),
            Color.parseColor("#AB1212")
    };

    private void initRing(Canvas canvas) {
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2);
        canvas.save();
        canvas.translate(canvas.getWidth() / 2, r);

        for (int i = 0; i < valueColors.length; i++) {
            paint.setStyle(Paint.Style.FILL);
            int[] colors = {valueColors[i], valueColors[i]};
            float[] positions = {0f, 1.0f};
            SweepGradient sweepGradient = new SweepGradient(0, 0, colors, positions);
            paint.setShader(sweepGradient);
            rect = new RectF(-length, -length, length, length);
            if (i == 0) {
                canvas.drawArc(rect, 170, 7 * 18 + 10, true, paint);
            } else if (i == 1) {
                canvas.drawArc(rect, 170 + 7 * 18 + 10, 3 * 9 , true, paint);
            } else if (i == 2) {
                canvas.drawArc(rect, 170 + 7 * 18 + 10 + 27, 9, true, paint);
            } else {
                canvas.drawArc(rect, 170 + 7 * 18 + 10 + 36, 2 * 9 + 10, true, paint);
            }
        }

        canvas.restore();
        canvas.save();
        canvas.translate(canvas.getWidth() / 2, r);

        strokePain = new Paint(paint);

        strokePain.setColor(0x3f979797);
        strokePain.setStrokeWidth(10);
        strokePain.setShader(null);
        strokePain.setStyle(Paint.Style.STROKE);
        //canvas.drawArc(rect, 170, 200, true, strokePain);


        canvas.restore();
        canvas.save();
        canvas.translate(canvas.getWidth() / 2, r);

        //底边水平
        paint.setShader(null);
        paint.setColor(backGroundColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(-length, (float) (Math.sin(Math.toRadians(10)) * length / 3f * 2f), length, (float) (Math.sin(Math.toRadians(10)) * length + 100), paint);
        //canvas.drawRect(-length, (float) (Math.sin(Math.toRadians(10)) * length / 3f * 2f), length, (float) (Math.sin(Math.toRadians(10)) * length / 3f * 2f), strokePain);


        //内部背景色填充
        paint.setColor(backGroundColor);
        paint.setShader(null);
        //rect = new RectF(-(length - length / 3f - 15), -(length / 3f * 2f - 15), length - length / 3f - 15, length / 3f * 2f - 15);
        rect = new RectF(-length+60, -length+60, length-60, length-60);

        //canvas.drawArc(rect, 170, 200, true, strokePain);
        canvas.drawArc(rect, 0, 360, true, paint);
    }


    public void cgangePer(float per) {
        this.perOld = this.per;
        this.per = per;
        ValueAnimator va = ValueAnimator.ofFloat(perOld, per);
        va.setDuration(1000);
        va.setInterpolator(new OvershootInterpolator());
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                perPoint = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        va.start();

    }
}